package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.PropComment;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.PropCommentQuery;
import cn.wolfcode.luowowo.comment.repository.PropCommentRepository;
import cn.wolfcode.luowowo.comment.service.IPropCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@Service
public class PropCommentServiceImpl implements IPropCommentService{

    @Autowired
    private PropCommentRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(PropComment pct) {
        repository.save(pct);
    }

    @Override
    public Page findAll(PropCommentQuery qo) {
        Query query = new Query();
        //分页条件,不要忘记减一
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");

        long count = template.count(query, PropComment.class);

        query.with(pageRequest);

        List<PropComment> list = template.find(query, PropComment.class);

        return new PageImpl(list,pageRequest, count);
    }
}
